<?php

namespace App\Console\Commands;

use App\Dao\CommonDao;
use App\Dao\UsersDao;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class User_Max_Recive extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user_max_recive';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '修改用户最大收益';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $query = UsersDao::builder_user_query()
            ->where('vip_index_level',1)
            ->where('max_recive','<>',0);
        if($query->exists()){
            $list = $query->get()->toArray();
            DB::beginTransaction();
            try {
                foreach ($list as $value){
                    //金币价值*单价+提现单价
                    $itemMoney = bcadd(bcmul($value['score'],CommonDao::real_time_price(),2),$value['w_prices'],2);
                    $bcAM = bcsub($value['max_recive_frozen'],$itemMoney,2);
                    if($bcAM != $value['max_recive']){
                        if($bcAM < 0){
                            $bcAM = 0;
                        }
                        UsersDao::builder_user_query([
                            'user_id'=>$value['user_id']
                        ])->update([
                            'max_recive'=>$bcAM
                        ]);
                    }
                }
                DB::commit();
            }catch (\Exception $exception){
                DB::rollBack();
            }
        }
    }
}
